package com.yidu.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.util.StringUtils;

import com.yidu.domain.Module;
import com.yidu.domain.vo.ModuleVo;
import com.yidu.utils.Pages;

public class ModuleDao implements ModuleDaoInter{
    private HibernateTemplate hibernateTemplate;
    
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public void add(Module module) {
		// TODO Auto-generated method stub
		hibernateTemplate.save(module);
	}

	@Override
	public void delete(String moduleId) {
		// TODO Auto-generated method stub
		Module module = findById(moduleId);
		hibernateTemplate.delete(module);
	}

	@Override
	public void update(Module module) {
		// TODO Auto-generated method stub
		hibernateTemplate.update(module);
	}

	@Override
	public List<Module> findAllPage(Module module, Pages page) {
		DetachedCriteria deta = DetachedCriteria.forClass(Module.class);
		if(!StringUtils.isEmpty(module.getModuleName())) {
			deta.add(Restrictions.ilike("moduleName", "%"+module.getModuleName()+"%"));
		}
		List<Module> list = (List<Module>) hibernateTemplate.findByCriteria(deta, page.getStartRows(), page.getLimit());
		return list;
	}

	@Override
	public Module findById(String moduleId) {
		return hibernateTemplate.get(Module.class, moduleId);
	}

	@Override
	public Long count(ModuleVo vo) {
		// TODO Auto-generated method stub
		DetachedCriteria deta = DetachedCriteria.forClass(Module.class);
		deta.setProjection(Projections.rowCount());
		if(!StringUtils.isEmpty(vo.getModuleName())) {
			deta.add(Restrictions.ilike("moduleName", "%"+vo.getModuleName()+"%"));
		}
		Long count = (Long) hibernateTemplate.findByCriteria(deta).get(0);
		return count;
	}
	/**
	 * 查询全部数据，不分页
	 */
	@Override
	public List<Module> findAll() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Module.class);
		List<Module> list = (List<Module>) hibernateTemplate.findByCriteria(detachedCriteria);
		return list;
	}
  
}
